home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / TOHEX.ASM < prev    next >
Assembly Source File  |  1991-10-23  |  3KB  |  156 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8.         extrn    sl_malloc:far
  9. ;
  10. ; ToHex-    Converts a sequence of binary bytes a string representing
  11. ;        that sequence in Intel Hex format.
  12. ;
  13. ; inputs:
  14. ;
  15. ;    ES:DI-    Points to the sequence of bytes to convert.
  16. ;    CX-    Number of bytes to convert.
  17. ;    BX-    Base address (load address) of bytes in hex format.
  18. ;
  19. ; Note: CX should be less than or equal to 32.
  20. ;
  21. ;
  22. ; outputs:
  23. ;
  24. ;    ES:DI-    Points at a zero terminated string on the heap
  25. ;
  26. ;    Carry flag is set on return if there was a memory allocation error.
  27. ;
  28. ; The hex string takes the following form:
  29. ;
  30. ;    ": BB LLHH RR DDDDD...DDD SS <cr> <lf>"
  31. ;
  32. ;    (note:spaces are added for readability only)
  33. ;
  34. ; BB     = byte count which represents the actual number of data bytes.
  35. ; LLHH    = L.O. & H.O. bytes of the load address (BX on entry).
  36. ; RR    = record type; 00= data record, 01= end record, 02=address record.
  37. ;                   (this code only outputs data records).
  38. ; D...D = data bytes.
  39. ; SS    = Check sum = -(sum of BB, LL, HH, RR, and data bytes)
  40. ;
  41. ;
  42.         public    sl_ToHex
  43. ;
  44. sl_ToHex    proc    far
  45.         push    ds
  46.         push    si
  47.         push    ax
  48. ;
  49.         mov    ax, es
  50.         mov    ds, ax
  51.         mov    si, di
  52. ;
  53. ; Compute the number of bytes we'll need for the hex string:
  54. ;    2*bytecnt + 13
  55. ;
  56.         push    cx
  57.         shl    cx, 1
  58.         add    cx, 13
  59.         call    sl_malloc
  60.         pop    cx
  61.         jc    BadToHex
  62.         push    es            ;Save ptr to string
  63.         push    di
  64. ;
  65.         mov    byte ptr es:[di], ":"
  66.         inc    di
  67. ;
  68.         mov    ah, 0            ;Init checksum
  69. ;
  70. ; Output RR field:
  71. ;
  72.         mov    al, cl            ;Get byte cnt
  73.         call    PutHex
  74. ;
  75. ; Output address here:
  76. ;
  77.         mov    al, bl
  78.         call    PutHex
  79.         mov    al, bh
  80.         call    PutHex
  81. ;
  82. ; Output the record type here:
  83. ;
  84.         mov    al, 0
  85.         call    PutHex
  86. ;
  87. ; Output the data bytes here:
  88. ;
  89. PutDataLp:    mov    al, [si]
  90.         call    PutHex
  91.         inc    si
  92.         loop    PutDataLp
  93. ;
  94. ; Okay, output the checksum here:
  95. ;
  96.         mov    al, ah            ;Get the checksum
  97.         neg    al            ;Negate it.
  98.         call    PutHex
  99. ;
  100. ; Output the CR LF at the end
  101. ;
  102.         mov    byte ptr es:[si], 13    ;CR
  103.         mov    byte ptr es:1[si], 10    ;LF
  104. ;
  105. ; And don't forget the zero terminating byte.
  106. ;
  107.         mov    byte ptr es:2[si], 0
  108.         clc
  109. ;
  110. ; Get address of start of string:
  111. ;
  112.         pop    di
  113.         pop    es
  114. ;
  115. ; Okay, quit!
  116. ;
  117. BadToHex:    pop    ax
  118.         pop    si
  119.         pop    ds
  120.         ret
  121. ;
  122. sl_ToHex    endp
  123. ;
  124. ;
  125. ; PutHex-    Adds AL to AH (to compute checksum), stores AL at ES:DI,
  126. ;        and increments DI by one.
  127. ;
  128. PutHex        proc
  129.         push    ax
  130.         mov    ah, al
  131.         shr    al, 1
  132.         shr    al, 1
  133.         shr    al, 1
  134.         shr    al, 1
  135.         call    PHex
  136.         mov    al, ah
  137.         and    al, 0fh
  138.         call    PHex
  139.         pop    ax
  140.         add    ah, al            ;Compute checksum
  141.         ret
  142. PutHex        endp
  143. ;
  144. PHex        proc
  145.         or    al, '0'
  146.         cmp    al, '9'
  147.         jbe    PutIt
  148.         add    al, 7
  149. PutIt:        mov    es:[di], al
  150.         inc    di
  151.         ret
  152. PHex        endp
  153. ;
  154. stdlib        ends
  155.         end
  156.